﻿@using Newbe.BookmarkManager.Services
<Space Direction="DirectionVHType.Horizontal">
    <SpaceItem>
        Hot Tags:
    </SpaceItem>
    @foreach (var tag in _tags)
    {
        <SpaceItem>
            <Badge Count="tag.ClickedCount"
                   Size="small"
                   Style="background-color: #52c41a; ">
                <ChildContent>
                    <Tag Closable="false"
                         OnClick="e => OnClickTag(tag)"
                         Color="@WebHelper.GetTagColor(tag.Tag)"
                         @key="@tag.Tag">
                        @tag.Tag
                    </Tag>
                </ChildContent>
            </Badge>
        </SpaceItem>
    }
</Space>

@code {

    private List<BkTag> _tags = new();

    [Parameter]
    public EventCallback<TagClickEventArgs> OnTagClick { get; set; }

    [Inject]
    public ITagsManager TagsManager { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        var tags = await TagsManager.GetHotAsync();
        _tags = tags;
    }

    public record TagClickEventArgs(BkTag Tag);

    private async Task OnClickTag(BkTag tag)
    {
        await OnTagClick.InvokeAsync(new TagClickEventArgs(tag));
#pragma warning disable 4014
        InvokeAsync(async () =>
        {
            var tags = await TagsManager.GetHotAsync();
            _tags = tags;
            StateHasChanged();
        });
#pragma warning restore 4014
    }
}